package com.yang.config;
import com.yang.service.NoteUserService;
import com.yang.service.impl.NoteUserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.Md4PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;
import java.io.PrintWriter;

/**
 * @author yangdacheng
 * @title: WebSecurityConfig
 * @projectName note_api
 * @description: TODO
 * @date 2022/4/9 8:55 上午
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyUserDetailsService myUserDetailsService;

    /**
     * 加密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * 授权
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/deposit/**","/noteEmail/**","/noteOrder/**",
                        "/noteRole/**","/noteThings/**","/pictureTable/**",
                        "/userNoteInfo/**","/userThings/**")
//                .hasAnyRole("superAdmin","admin","user")
                .permitAll()
                .antMatchers("/superAdmin/**")
//                .hasRole("superAdmin")
                .permitAll()
                .antMatchers("/noteList/**","/noteUser/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .cors()
                .and().formLogin().permitAll()
                .and()
                .rememberMe()
                .and()
                .csrf().disable();
    }

    /**
     * 认证
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
    }


}
